<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>角色信息快照</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">选择区服:</label>
                            <div class="layui-input-inline">
                                <select name="gameArea" class="layui-select">
                                    <option v-for="(item,index) in services" :value="item.server_id" :selected="index==0">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleId" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">快照时间:</label>
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="beginDateTime" id="beginDateTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endDateTime" id="endDateTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="cwxx" >宠物信息</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="zbxx" >装备信息</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="bbxx" >背包信息</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="grck" >个人仓库</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="bt" >摆摊</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="jlbb" >精灵背包</a>
</script>
<script type="text/html" id="toolbarDemo">
    <form class="layui-form" id="deleteByTimeForm" auth="roleSnapshoot:deleteByDateTime">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" name="dateTime" value="" id="deleteTime" class="layui-input" />
            </div>
            <button class="layui-btn layui-btn-danger layui-btn-sm" type="submit">删除小于此时间的快照</button>
        </div>
    </form>
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    var services = JSON.parse(localStorage.getItem('services'))

    new Vue({
        el: "#app",
        data: {
            services:services,
            tableDataList:[]
        },
        mounted:function () {
            layui.use(['form', 'table','laydate'], function () {
                var $ = layui.jquery,
                    form = layui.form,
                    laydate = layui.laydate,
                    table = layui.table;
                var refreshInterval = 5000; // 刷新间隔为 5 秒
                var refreshTimeout;
                function refreshTable() {
                    // 刷新表格的操作
                    table.reload('currentTableId');
                }
                laydate.render({
                    elem: '#beginDateTime',
                    value: getYmd(0)
                });
                laydate.render({
                    elem: '#endDateTime',
                    value: getYmd(1),
                });

                table.render({
                    elem: '#currentTableId',
                    url: '/admin/roleSnapshoot/paginate',
                    toolbar: '#toolbarDemo',
                    where: form.val('table-search'),
                    defaultToolbar: ['filter', 'print'],
                    cols: [[
                        {type: "checkbox", width: 50},
                        {field: 'id', title: '角色ID', sort: true},
                        {field: 'job', title: '职业',templet:function(row){
                                switch (row.job) {
                                    case 1: return '战士';
                                    case 2: return '法师';
                                    case 3: return '射手';
                                    case 4: return '猎人';
                                    case 11: return '防御战士';
                                    case 12: return '狂暴战士';
                                    case 13: return '守护战士';
                                    case 21: return '元素法师';
                                    case 22: return '治疗法师';
                                    case 23: return '诅咒法师';
                                    case 31: return '射术猎人';
                                    case 32: return '隐匿猎人';
                                    case 33: return '鸿飞猎人';
                                    default: return '未知职业'; // 处理未匹配的情况
                                }
                            }},

                        {field: 'curexp', title: '当前经验'},
                        {field: 'nick', title: '角色昵称'},
                        {field: 'sex', title: '性别',templet:function (row) {
                            return row.sex===1?'男':'女'
                            }},

                        {field: 'snap_datetime', title: '快照时间'},
                        {title: '操作', minWidth: 470, align: "center" ,toolbar: '#currentTableBar',}
                    ]],
                    done:function(res,curr){
                    if(res.heap>72){
                        // 2秒后关闭消息提示
                        setTimeout(function() {
                            alert('数据运算压力较大,请稍后重试！');
                        }, 5000); // 1000毫秒等于1秒

                    }
                },
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line'
                });

                // 监听分页事件，当用户切换页数或更改每页显示数量时触发
                table.on('page(currentTableId)', function(obj) {
                    // 清除之前的延迟
                    clearTimeout(refreshTimeout);
                    // 设置新的延迟
                    refreshTimeout = setTimeout(refreshTable, refreshInterval);
                });

                laydate.render({
                    elem:"#deleteTime",
                    type:"datetime"
                })

                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    var searchData = data.field;
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: searchData
                    }, 'data');
                    return false;
                });

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'cwxx') {
                        var filename = data.id +'-'+ data.nick+'-' + '宠物信息'+'-' + data.snap_datetime;
                        filename += '.json';
                        downloadText(filename,JSON.stringify(data.pet))
                    } else if(obj.event==='zbxx'){
                        var filename = data.id +'-'+ data.nick+'-' + '装备信息'+'-' + data.snap_datetime;
                        filename += '.json';
                        downloadText(filename,JSON.stringify(data.combat))
                    } else if(obj.event==='bbxx'){
                        var filename = data.id +'-'+ data.nick+'-' + '背包信息'+'-' + data.snap_datetime;
                        filename += '.json';
                        downloadText(filename,JSON.stringify(data.bag))
                    } else if(obj.event==='grck'){
                        var filename = data.id +'-'+ data.nick+'-' + '个人仓库'+'-' + data.snap_datetime;
                        filename += '.json';
                        downloadText(filename,JSON.stringify(data.warehouse))
                    } else if(obj.event==='bt'){
                        if (!data.stall){
                            layer.msg('没有摆摊数据')
                            return;
                        }
                        var filename = data.id +'-'+ data.nick+'-' + '摆摊'+'-' + data.snap_datetime;

                        filename += '.json';
                        downloadText(filename,JSON.stringify(data.stall))
                    } else if(obj.event==='jlbb'){
                        var filename = data.id +'-'+ data.nick+'-' + '精灵背包'+'-' + data.snap_datetime;

                        filename += '.json';
                        downloadText(filename,data.spiritbag)
                    }
                });

                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'add') {  // 监听添加操作
                        var index = layer.open({
                            title: '添加',
                            type: 2,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'add.html',
                        });
                    }
                });

                $('body').on('submit','#deleteByTimeForm',function(){
                    var dateTime = $('#deleteTime').val()
                    layer.confirm('您确定删除吗?',{icon:3},function () {
                        $.post('/admin/roleSnapshoot/deleteByDateTime',{_method:'delete',dateTime:dateTime},function (res) {
                            if (res.code === 0 ) {
                                layer.msg('删除成功',{icon:1})
                            }
                        })
                    })
                    return false;
                })
            });
        }
    })
</script>
</body>
</html>